import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:series_play/widget/app_color.dart';
import 'package:series_play/widget/dialog/AppSimpleDialog.dart';
import 'package:series_play/widget/dialog/drama_info_dialog.dart';
import 'package:series_play/widget/dialog/video_resolution_dialog.dart';
import '../widget/dialog/ConnectServiceDialog.dart';
import '../widget/dialog/video_speed_dialog.dart';

class DialogHelper {
  DialogHelper._();

  static Container _styleContainer(
    Widget child, {
    EdgeInsetsGeometry? margin,
    EdgeInsetsGeometry? padding,
    Color? color,
    BorderRadius? radius,
    BoxDecoration? boxDecoration,
  }) {
    return Container(
      margin: margin ?? const EdgeInsets.symmetric(horizontal: 16).w,
      padding: padding ?? const EdgeInsets.all(16).w,
      decoration:
          boxDecoration ??
          BoxDecoration(
            color: color ?? const Color(0xFF292B34),
            borderRadius: radius ?? BorderRadius.circular(10).r,
          ),
      child: child,
    );
  }

  /// 居中样式
  static Widget _centerContent(
    Widget child, {
    EdgeInsetsGeometry? margin,
    EdgeInsetsGeometry? padding,
    Color? color,
    BorderRadius? radius,
  }) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          _styleContainer(
            child,
            margin: margin,
            padding: padding,
            color: color,
            radius: radius,
          ),
        ],
      ),
    );
  }

  /// 公共底部弹框样式
  ///
  /// 头部有个横条
  static Container _styleBottomContent(
    Widget child, {
    EdgeInsetsGeometry? padding,
  }) {
    return Container(
      padding: padding,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            margin: const EdgeInsets.symmetric(vertical: 10).w,
            height: 3.w,
            width: 48.w,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(2).r,
              color: AppColor.white_20,
            ),
          ),
          child,
        ],
      ),
    );
  }

  /// 缩放动画
  static ScaleTransition _scaleAnimation(BuildContext context, Widget child) {
    return ScaleTransition(
      scale: CurvedAnimation(
        parent: ModalRoute.of(context)!.animation!,
        curve: Curves.ease,
      ),
      child: child,
    );
  }

  /// 简单文本提示
  ///
  /// 标题
  /// 内容
  /// 取消 / 确认
  static void showSimpleDialog(
    BuildContext context, {
    String? title,
    String? content,
    Widget? child,
    Widget? suffixChild,
    String? cancelText = "取消",
    String? confirmText = "确定",
    VoidCallback? onConfirm,
    VoidCallback? onCancel,
    EdgeInsetsGeometry? padding,
    Color? color,
    BorderRadius? radius,
  }) {
    showDialog(
      context: context,
      builder: (_) {
        return _scaleAnimation(
          context,
          _centerContent(
            AppSimpleDialog(
              title: title,
              content: content,
              confirmText: confirmText,
              cancelText: cancelText,
              onConfirm: onConfirm,
              onCancel: onCancel,
              suffixChild: suffixChild,
              child: child,
            ),
            padding: padding,
            color: color,
            radius: radius,
          ),
        );
      },
    );
  }

  /// 底部弹框
  ///
  /// 是否要 加入同一内边距
  static void showAppBottomSheet(
    BuildContext context, {
    bool isDismissible = true,
    Color? color,
    BorderRadius? radius,
    required WidgetBuilder builder,
    bool expand = false,
  }) {
    showMaterialModalBottomSheet(
      context: context,
      expand: expand,
      backgroundColor: color ?? AppColor.bottomSheetBgColor,
      isDismissible: isDismissible,
      shape: RoundedRectangleBorder(
        borderRadius:
            radius ??
            BorderRadius.only(
              topLeft: Radius.circular(10).r,
              topRight: Radius.circular(10).r,
            ),
      ),
      builder: builder,
    );
  }

  /// 客服弹窗
  static void showConnectServiceDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) {
        return _scaleAnimation(context, _centerContent(ConnectServiceDialog()));
      },
    );
  }

  /// 分辨率切换弹框
  static void switchResolutionDialog(BuildContext context) {
    showAppBottomSheet(
      context,
      builder: (context) {
        return _styleBottomContent(
          VideoResolutionDialog(),
          padding: EdgeInsets.only(
            bottom: context.mediaQueryPadding.bottom + 16.w,
          ),
        );
      },
    );
  }

  /// 视频播放速度切换
  static void switchVideoSpeedDialog(BuildContext context) {
    showAppBottomSheet(
      context,
      builder: (context) {
        return _styleBottomContent(
          VideoSpeedDialog(),
          padding: EdgeInsets.only(
            bottom: context.mediaQueryPadding.bottom + 16.w,
          ),
        );
      },
    );
  }

  /// 视频详情dialog
  ///
  /// 简介与选集
  static void showDramaInfoSheetDialog(BuildContext context) {
    showAppBottomSheet(
      context,
      builder: (context) {
        return _styleBottomContent(
          DramaInfoDialog(),
          padding: EdgeInsets.only(
            bottom: context.mediaQueryPadding.bottom + 16.w,
          ),
        );
      },
    );
  }
}
